#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <optra/zoneinterface.h>
#include "zonesmanager.h"
#include "proxyimpl.h"
#include "optrzone.h"
#include <QCloseEvent>
#include <optra/common/logevent.h>
#include <optra/common/gui.h>
#include <QCoreApplication>
#include <tasksmanager.h>
#include <optra/zoneinfo.h>
#include "applicationsmanager.h"
#include "windowholderui.h"
#include "customproxy.h"
#include "testwindow.h"

#include <optra/common/guiwindow.h>

//Optra::Proxy* proxy = NULL;

MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
{

    ui->setupUi(this);


      connect(zones.data(), SIGNAL(zonesLaunched()), SLOT(zonesInitialized()));

    connect(ui->next, SIGNAL(clicked()),  ui->viewParent, SLOT(next()));
    connect(ui->previous, SIGNAL(clicked()),  ui->viewParent, SLOT(previous()));
    connect(ui->pushButton, SIGNAL(clicked()),  ui->viewParent, SLOT(single(0)));

    zones->loadZones();
    //QObject::connect(proxy->gui(), SIGNAL(showTimedInfoWindow(QString, int)), zones->optra(), SLOT(showTimedInformationWindow(QString,int)));
}

MainWindow::~MainWindow()
{
    delete tray_;
    delete ui;    
    zones.clear();
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::closeEvent(QCloseEvent* e)
{    
//    foreach(ZoneInfo::ZoneWindow* w, views_){
//        w->widget()->remove();
//        delete w->widget();
//    }

    foreach(Optra::ZoneWindow* w, tools_){
        w->widget()->remove();
        delete w->widget();
    }


    testWindow()->close();
    zones->unloadZones();
    e->accept();
}


void MainWindow::zonesInitialized()
{
    ZonesManager::ZoneProcessMap views = zones->zoneProcess();

    QMenu * tm = new QMenu(this);

    int x = 0;
    int y = 0;
    foreach(ZoneProcess z, views.values()){


        Optra::ZoneInfo* zi = z.info;
        QMenu* zoneMenu = menuBar()->addMenu(zi->name());

        tm->addMenu(zoneMenu);


        if (zi->menu() != NULL){            
            zoneMenu->addMenu(zi->menu());
        }

        zoneMenu->addSeparator();

        QMenu *viewsmenu = zoneMenu->addMenu("Views");
        Optra::ZoneInfo::ViewsList wins = z.info->views();

        foreach(Optra::ZoneWindow* w, wins){

            w->widget()->setup();

           // w->widget()->setParent(ui->viewParent);

            QAction* showAction = new QAction("Show", this);
            showAction->setData(w->id());

            QMenu * tmm = viewsmenu->addMenu(w->display());
            tmm->addAction(showAction);

            if (w->menu() != NULL){
                tmm->addSeparator();
                tmm->addMenu(w->menu());
            }

            CustomProxy* cp = new CustomProxy(0, Qt::ToolTip);
            w->widget()->setWindowTitle(w->id());

            WindowHolderUi* wh = new WindowHolderUi(0, w->widget());
            //cp->setWidget(w->widget());
             cp->setWidget(wh);

            QRectF rect = cp->boundingRect();

            cp->setPos(x * rect.width() * 1.05, y * rect.height() * 1.05);
            cp->setCacheMode(QGraphicsItem::DeviceCoordinateCache);

            w->widget()->update();

            ui->viewParent->addWidgetProxy(cp);

            connect(tmm, SIGNAL(triggered(QAction*)), SLOT(showViewWindow(QAction*)));
            //vm->addMenu(wi.display);//->addMenu(wi.menu);

            x++;
            //y++;
        }

        QMenu *tm = zoneMenu->addMenu("Tools");
        Optra::ZoneInfo::ToolsList tools = z.info->tools();
        foreach(Optra::ZoneWindow* w, tools){

            w->widget()->setup();

            QAction* a = new QAction("Show", this);
            a->setData(w->id());

            QMenu * tmm = tm->addMenu(w->display());
            tmm->addAction(a);
            connect(tmm, SIGNAL(triggered(QAction*)), SLOT(showToolWindow(QAction*)));

            if (w->menu() != NULL){
                tmm->addSeparator();
                tmm->addMenu(w->menu());
            }

            tools_.append(w);
        }

    }
    zones->optra()->debug(QString("Registered %1 tools").arg(tools_.count()));


    tray_->setContextMenu(tm);
    tray_->show();

    zones->optra()->go();
}

void MainWindow::resizeEvent(QResizeEvent *e)
{
    Q_UNUSED(e);
}

void MainWindow::showToolWindow(QAction * a)
{
    QString id = a->data().toString();
    zones->optra()->debug(QString("ShowTool window command %1").arg(id));
    tool(id);
}

void MainWindow::showViewWindow(QAction * a)
{
    QString id = a->data().toString();
    zones->optra()->debug(QString("ShowView window command %1").arg(id));
    view(id);
}


void MainWindow::tool(const QString& id)
{
    for(int i = 0; i < tools_.count(); ++i){
        Optra::ZoneWindow* w = tools_[i];
        if (id == w->id()){
            w->widget()->beforeShow();
            w->widget()->update();
            w->widget()->show();
            break;
        }
    }
}

void MainWindow::view(const QString& id)
{

}
